package com.bw.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsCOntroller {
    @Autowired
    TmsService tmsService;
    @RequestMapping("/findTotal")
    public Object findTotal( String newDate){
//        1.拿到数据库中数据 [{c=140}, {c=176}, {c=16}]
        List<Map> list = tmsService.findTotal(newDate);
        System.out.println(list);
//        2.返回大屏所需要的数据
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray jsonArray = new JSONArray();


        JSONObject dataObject = new JSONObject();
        dataObject.put("name","接单数");
        dataObject.put("value",list.get(0).get("c"));
        jsonArray.add(dataObject);

        JSONObject dataObject1 = new JSONObject();
        dataObject1.put("name","发单数");
        dataObject1.put("value",list.get(1).get("c"));
        jsonArray.add(dataObject1);


        JSONObject dataObject2 = new JSONObject();
        dataObject2.put("name","完成运单数");
        dataObject2.put("value",list.get(2).get("c"));
        jsonArray.add(dataObject2);


        jsonObject.put("data",jsonArray);

        return jsonObject;
    };

    @RequestMapping("/findPro")
    public Object findPro( String newDate){
//        1.拿到数据库中数据 [{c=140}, {c=176}, {c=16}]
        List<Map> list = tmsService.findPro(newDate);
        System.out.println(list);
//        2.返回大屏所需要的数据
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray jsonArray = new JSONArray();
        for (Map map : list) {
            JSONObject dataObject = new JSONObject();
            dataObject.put("name", map.get("name"));
            dataObject.put("value",map.get("zb"));
            jsonArray.add(dataObject);
        }

        jsonObject.put("data",jsonArray);

        return jsonObject;
    };

    @RequestMapping("/findCity")
    public Object findCity( String newDate, String status,String name){
//        1.拿到数据库中数据 [{c=140}, {c=176}, {c=16}]
        List<Map> list = tmsService.findCity(newDate,status,name);
        System.out.println(list);
//        2.返回大屏所需要的数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();
//        x轴
        JSONArray XArray = new JSONArray();
        for (Map map : list) {
            XArray.add(map.get("name"));
        }
        dataObject.put("categories",XArray);
//        Y轴
        JSONArray YArray = new JSONArray();
        JSONObject YObject = new JSONObject();
        YObject.put("name","各城市");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("c"));
        }

        YObject.put("data",dataArray);

        YArray.add(YObject);
        dataObject.put("series",YArray);

        jsonObject.put("data",dataObject);

        return jsonObject;
    };

    @RequestMapping("/findDistance")
    public Object findDistance(String newDate, String status){
//        1.拿到数据库中的数据
        List<Map> list = tmsService.findDistance(newDate, status);
//        2.返回大屏所需要的数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
//       表头
        JSONArray colArray = new JSONArray();

        JSONObject colObject = new JSONObject();
        colObject.put("name","各机构的名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","完成的运输距离");
        colObject1.put("id","module");
        colArray.add(colObject1);

        dataObject.put("columns",colArray);
//       行值
        JSONArray rowArray = new JSONArray();
//        多行数据遍历
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("name"));
            rowObject.put("module",map.get("total"));
            rowArray.add(rowObject);
        }

        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
         return jsonObject;
    }

}
